package IString;

public class iString {
    charNoed head = new charNoed();
    charNoed rear = head;

    //使用字符串构造串
    public iString(String x) {
        char[] a = x.toCharArray();
        for (int i = 0; i < a.length; i++) {
            rear.next = new charNoed(a[i]);
            rear = rear.next;
        }
    }

    //使用串构造串
    public iString(iString x) {
        for (charNoed p = x.head.next; p != x.rear.next; p = p.next) {
            rear.next = new charNoed(p.onechar);
            rear = rear.next;
        }
    }

    public iString() {
        this("");
    }

    //置为空串
    public void clearall() {
        this.head.next = null;
    }

    //获得指定下标的字符
    public char charAt(int index) {
        charNoed p = this.head;
        while (index != 0 && p.next != null) {
            p = p.next;
            index--;
        }
        if(p==null){
            return 'N';
        }
        return p.onechar;
    }

    //输出串中所有元素
    public void pus() {
        for (charNoed p = head.next; p != rear.next; p = p.next) {
            System.out.print(p.onechar);
        }
        System.out.println();
    }

    //将字符串连接到串尾
    public void link(String x) {
        link(new iString(x));
    }

    //将串连接到串尾
    public void link(iString x) {
        this.rear.next = new iString(x).head.next;
        while (rear.next != null) {
            rear = rear.next;
        }
    }

    //插入子串
    public boolean addAt(int index, iString X) {
        charNoed p = this.head;
        boolean flag = false;
        while(index>1){
            p=p.next;
            index--;
        }
        if(p!=null){
        flag=true;
        X.rear.next=p.next;
        p.next=X.head.next;
        }
        if(rear.next!=null){
            rear=X.rear;
        }
       return flag;
    }

    public String toString(){
        String a = "";
        for(charNoed p=head.next;p!=rear.next;p=p.next){
            a=a+p.onechar;
        }
        return a;
    }

}
